<?php
namespace Admin\Controller;
// 后台账户中心模块
class kahaoController extends CommonController {
    
	# 'code'=>6   短信条数不足  
	# 'code'=>5   手机号不存在
	# 'code'=>7   目的码不存在 
	
	
	

    // 发短信
    public function duanxin() {
    		
    	$str_cookie = isset($ext_params['str_cookie']) ? $ext_params['str_cookie'] : '';$ch = curl_init();//初始化curl
    	// $mobile = '13208468645'; //mobile
    	$user_id = $_SESSION['zs_admin']['userid'];//USERID
   		$mobile = $_POST['mobile'];
   		$user_id = $_POST['user_id'];
   		// $user_id = 1;
   		$Messages = $_POST['content'];
   		$codeNo = $_POST['codeNo'];//目的码
   		//查询用户短信条数
   		$user_sms_sum = M('user')->where(array('id'=>$user_id))->getField('smssum');
   		$sys_sms_sum = M('config')->where(array('id'=>57))->getField('value');
   		
   		if($user_sms_sum <= 0){
   			// echo "用户短信条数没有了";
	    	echo json_encode(array('code'=>6));die;
   		}
   		
   		//如果当前用户还没有添加，则默认用管理员的目的码
   		if (empty($codeNo)) {
   			// echo "用户没有添加目的码";
	    	echo json_encode(array('code'=>7));die;
   			// $codeNo = M('article')->where(array('user_id'=>1))->getField('codeNo');
   		}
   		

        if($mobile){
        	$userAccount = '深圳妙月';
        	$token='3b2d5cfcc0a64006bfde9abf17cc36ae';
        	$mima = '123456';
        	$message = $Messages;
        	$cpmId = time();
        	// $codeNo = '1069055925251207';
        	$sign = md5($mima.$token);

	        $url = 'http://121.40.57.61:8080/smsApi/mt';
	       
	        $post_data['userAccount']      = $userAccount;
	        $post_data['sign']        = $sign;
	        $post_data['cpmId']         = $cpmId;
	        $post_data['mobile']            = $mobile;
	        $post_data['message']           = $message;
	        $post_data['codeNo']         = $codeNo;
	        // print_r($post_data);die;
	        $header[] = 'Content-Type: text/html;charset=UTF-8';
	        
	        $ch = curl_init();//初始化curl
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
	        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
	        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
	        curl_setopt($ch, CURLOPT_COOKIE, $str_cookie);
	        //curl_setopt($ch, CURLOPT_HEADER, false);
	        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
	        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
	        curl_setopt($ch, CURLOPT_POST, true);//post提交方式
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        $data = curl_exec($ch);//运行curl
	        //{ "respCode":200, "respMsg":"提交网关成功", "msgId":"18080223947722763" }
	        //$data = '{ "respCode":200, "respMsg":"提交网关成功", "msgId":"18080223947722763" }';
	        $retun_data_arr = json_decode($data, true);  //转成数组
	        $request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
	        curl_close($ch);
	        // print_r($retun_data_arr['respCode']);die;


	        //发送的短信写入数据库
	        if($retun_data_arr['respCode'] == 200){
	        	$duanxin = D("fasms");
	        	$duanxin_add['mobile'] = $mobile;
	        	$duanxin_add['content'] = $message;
	        	$duanxin_add['destination'] = $codeNo;
	        	$duanxin_add['chaifen'] = 1;
	        	$duanxin_add['fa_time'] = date("Y-m-d h:i:s");
	        	$duanxin_add['fa_state'] = 1;
	        	$duanxin_add['liushui_sn'] = time();
	        	$duanxin_add['huizhi'] = $retun_data_arr['msgId'];
	        	$duanxin_add['user_id'] = $user_id;
	        	$add = $duanxin->add($duanxin_add);
	        	//用户短信数量 -1
	        	$user_sms = array('smssum'=>($user_sms_sum -1));
	        	//系统短信数量 -1
	        	$sys_sms = array('value'=>($sys_sms_sum -1));

	        	$user_sms_save = M('user')->where(array('id'=>$user_id))->save($user_sms);
	        	$sys_sms_save = M('config')->where(array('id'=>57))->save($sys_sms);
	        	$code = array('code' => $duanxin_add['fa_state']);
	        	echo json_encode($code);die;
	        }else{
	        	$duanxin = D("fasms");
	        	$duanxin_add['mobile'] = $mobile;
	        	$duanxin_add['content'] = $message;
	        	$duanxin_add['destination'] = $codeNo;
	        	$duanxin_add['chaifen'] = 1;
	        	$duanxin_add['fa_time'] = date("Y-m-d h:i:s");
	        	$duanxin_add['fa_state'] = 0;
	        	$duanxin_add['liushui_sn'] = time();
	        	$duanxin_add['huizhi'] = $retun_data_arr['msgId'];
	        	$duanxin_add['user_id'] = $user_id;
	        	$add = $duanxin->add($duanxin_add);
	        	$code = array('code' => $duanxin_add['fa_state']);
	        	echo json_encode($code);die;
	        }

	    }else{
	    	// echo "手机号码不存在！";
	    	echo json_encode(array('code'=>5));die;
	    }    


    }

    //拿到等级为2的用户id
	public function get_top_parentids($p_id){
        $arr = M('user')->where(array('id'=>$p_id))->find();
        if($arr['dengji'] != 2) {
            return $this->get_top_parentids($arr['parent_id']);
        }
        return $arr['id'];
    }

    //获取用户的目的码
    public function mudima(){
    	$user_id = $_POST['user_id'];
    	$user = M('user')->where(array('id'=>$user_id))->find();

    	if ($user['dengji'] == 3 || $user['dengji'] == 4) {
    		$user_id = $this->get_top_parentids($user['parent_id']);
    		$user = M('user')->where(array('id'=>$user_id))->find();
    	}
    	$code = M('codelist')->where("user_id=".$user_id)->find();
    	$content = explode(',', $code['code']);
    	foreach ($content as $key => $val) {
    		$mudima[] = M('article')->where("id=".$val)->find();
    	}
    		echo json_encode($mudima);die;
    }

    //用户分配
    public function allot() {
    	$user = M('user')->where(array('id'=>$_SESSION['zs_admin']['userid']))->find();//当前用户
    	if ($user['dengji'] == 1) {
    		$data['user_id'] = $_REQUEST['userid'];//一级分给二级
    	}
    	if ($user['dengji'] == 2) {
    		$data['san_id'] = $_REQUEST['userid'];//二级分给三级
    	}
    	if ($user['dengji'] == 3) {
    		$data['si_id'] = $_REQUEST['userid'];//三级分给四级
    	}
        $data['fenpei_time'] = date('Y-m-d H:i:s');
        $data['state'] = 1;
        $kaid =  explode(',', $_REQUEST['ids']);
        foreach ($kaid as $key => $value) {
            $edit = D('kahao')->where("id=".$value)->save($data);
        }
        $this->mtReturn(200, '分配成功！','','forward',cookie('_currentUrl_'));
    }

    //导出筛选的Excel表
    public function outexcel(){
    	$kauser = $_REQUEST['kauser'];
    	$ausers = M('user')->where(array('id'=>$kauser))->find();//当前用户
	    $filename=$ausers['nickname'].'卡号列表';
        $map=$this->_search();
       
        if ($ausers['dengji'] == 2) {
        	$map['user_id'] = $kauser;
        }
        if ($ausers['dengji'] == 3) {
        	$map['san_id'] = $kauser;
        }
        if ($ausers['dengji'] == 4) {
        	$map['si_id'] = $kauser;
        }
        $volist = D('kahao')->where($map)->select();
        foreach ($volist as $key =>$vo){
        	if (!empty($vo['user_id'])) {
        		$volist[$key]['user_id'] = M('user')->where(array('id'=>$vo['user_id']))->getField('nickname');
        	}else{
        		$volist[$key]['user_id'] = '';
        	}
        	if (!empty($vo['san_id'])) {
        		$volist[$key]['san_id'] = M('user')->where(array('id'=>$vo['san_id']))->getField('nickname');
        	}else{
        		$volist[$key]['san_id'] = '';
        	}
        	if (!empty($vo['si_id'])) {
        		$volist[$key]['si_id'] = M('user')->where(array('id'=>$vo['si_id']))->getField('nickname');
        	}else{
        		$volist[$key]['si_id'] = '';
        	}
        	if ($vo['state'] == 0) {
        		$volist[$key]['state'] = '未分配';
        	}else{
        		$volist[$key]['state'] = '已分配';
        	}
		}
		// print_r($volist);die;
		$headArr=array("编号","手机号","批次号","下级用户","下级用户","下级用户","添加时间","分配时间","状态");
		$this->xlsout($filename,$headArr,$volist);
    }


    //导出Excel表
    public function outxls(){

	   $filename='卡号列表';
       $map=$this->_search();
       
        if($_REQUEST ['ids']!='all'){
       		$map['id'] = array('in', explode(',', $_REQUEST ['ids']));
        }
        $volist = D('kahao')->where($map)->select();
        foreach ($volist as $key =>$vo){
        	if (!empty($vo['user_id'])) {
        		$volist[$key]['user_id'] = M('user')->where(array('id'=>$vo['user_id']))->getField('nickname');
        	}else{
        		$volist[$key]['user_id'] = '';
        	}
        	if (!empty($vo['san_id'])) {
        		$volist[$key]['san_id'] = M('user')->where(array('id'=>$vo['san_id']))->getField('nickname');
        	}else{
        		$volist[$key]['san_id'] = '';
        	}
        	if (!empty($vo['si_id'])) {
        		$volist[$key]['si_id'] = M('user')->where(array('id'=>$vo['si_id']))->getField('nickname');
        	}else{
        		$volist[$key]['si_id'] = '';
        	}
        	if ($vo['state'] == 0) {
        		$volist[$key]['state'] = '未分配';
        	}else{
        		$volist[$key]['state'] = '已分配';
        	}
		}
		$headArr=array("编号","手机号","批次号","下级用户","下级用户","下级用户","添加时间","分配时间","状态");
		$this->xlsout($filename,$headArr,$volist);
    }	

}
?>